<template>
  <div class="login-box">
      <div class="login-container-header">佰年鸿</div>
    <div class="login-container">
      <div class="login-container-box">
    <el-form ref="loginForm" v-if="loginType == 'username'" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">

      <div class="title-container">
        <ul>
          <li @click="loginType = 'mobile'">验证码登录</li>
          <li  style="color: #1890ff;">密码登录</li>
        </ul>
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="账户/电子邮件/手机"
          name="username"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="登录密码"
            name="password"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
      </el-tooltip>
      <div class="login-protocle">
          注册登录即代表已阅读并同意我们的<a href="">用户协议</a>与<a href="">隐私政策</a>
      </div>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
      
      <div style="position:relative">
        <div class="tips" style="color: #1890ff;">
          <span>忘记密码?</span>
          <span @click="loginType = 'mobile'">立即注册</span>
        </div>
      </div>
    </el-form>
    <el-form ref="loginMobileForm" v-if="loginType == 'mobile'" :model="loginMobileForm" :rules="loginMoibleRules" class="login-form" autocomplete="on" label-position="left">

      <div class="title-container">
        <ul>
          <li style="color: #1890ff;">验证码登录</li>
          <li @click="loginType = 'username'" >密码登录</li>
        </ul>
      </div>
      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="mobile"
          v-model="loginMobileForm.mobile"
          placeholder="手机号码"
          name="mobile"
          type="text"
          tabindex="1"
          autocomplete="on"
        />
      </el-form-item>

      <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
        <el-form-item prop="code">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            ref="code"
            v-model="loginMobileForm.code"
            placeholder="验证码"
            name="code"
            tabindex="2"
            autocomplete="on"
            @keyup.native="checkCapslock"
            @blur="capsTooltip = false"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            获取验证码
          </span>
        </el-form-item>
      </el-tooltip>

      <div class="login-protocle">
          注册登录即代表已阅读并同意我们的<a href="">用户协议</a>与<a href="">隐私政策</a>，未注册的手机号将自动注册

      </div>
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleMobileLogin">登录</el-button>

    </el-form>

    <el-dialog title="Or connect with" :visible.sync="showDialog">
      Can not be simulated on local, so please combine you own business simulation! ! !
      <br>
      <br>
      <br>
      <social-sign />
    </el-dialog>
      </div>
      <div class="login-container-qrcode">
          <div class="login-container-qrcode-icon">
            <img src="/" />
          </div>
          <div class="login-container-qrcode-text">
            <div></div>
            <div>微信扫码登录</div>
          </div>
      </div>
  </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import SocialSign from './components/SocialSignin'

export default {
  name: 'Login',
  components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入您的账户信息'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请输入账户登录密码'))
      } else {
        callback()
      }
    }
    return {
      loginType:'username',
      loginMobileForm:{
        mobile:"",
        code:""
      },
      loginForm: {
        username: 'admin',
        mobile:"",
        password: '123456'
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loginMoibleRules: {
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      currUserName:""
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    let _userNmae = window.localStorage.getItem('loginUserName');
    if(_userNmae.length > 0){
      this.loginForm.username = _userNmae
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    handleWechatLogin:function(){

    },
    wechatLoginValied:function(url){
      
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      let _this = this
      _this.currUserName = window.localStorage.getItem('loginUserName');
      if(_this.currUserName != this.loginForm.username){
        _this.redirect = '';
      }
      console.log(window.location.href);
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          window.localStorage.setItem('loginUserName',this.loginForm.username)
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
                this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch((res) => {
              console.log(">>>>",res)
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleMobileLogin:function(){
      let _this = this
      _this.currUserMoible = window.localStorage.getItem('loginUserMoible');
      if(_this.currUserMoible != this.loginMobileForm.mobile){
        _this.redirect = '';
      }
      console.log("手机登录",window.location.href);
      this.$refs.loginMobileForm.validate(valid => {
        if (valid) {
          this.loading = true
          window.localStorage.setItem('loginUserMoible',this.loginMobileForm.mobile)
          //console.log(this.loginMobileForm)
          //return false
          this.$store.dispatch('user/loginByMobile', this.loginMobileForm)
            .then(() => {
                this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch((res) => {
              console.log(">>>>",res)
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#000;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-box { background: #000; min-width: 100%; height: 100%; padding-top: 60px;}

.login-protocle { padding: 15px 0; color: #999; font-size: 12px; margin-bottom: 50px;}
.login-protocle a { color: #ededed; text-decoration:underline; margin: 0 5px;}

.title-container ul  { display: flex; margin: 0 auto; padding: 0; width: 200px; }
.title-container ul li { list-style: none; padding: 15px 10px; color: #fff;}

.login-container-header { width:600px; margin:0 auto; text-align: center; font-weight: bold; padding:15px 0; color: #1890ff; }
.login-container-header { font-size: 32px; padding-bottom: 30px;}

.login-container-qrcode { width: 280px; padding-top: 80px; border-radius: 10px; background: rgba(255,255,255,.03); }
.login-container-qrcode-icon { width: 160px; height: 160px; margin: 0 auto; background: #fff; border-radius: 10px;}
.login-container-qrcode-text { width: 160px; height: 50px; padding-top: 25px; margin: 0 auto; text-align: center; color: #fff;}
.login-container-qrcode-text { font-size:14px;}
.login-container {
  width: 726px;
  min-width: 726px;
  border-radius: 10px;
  margin: 0 auto;
  background: rgba(255,255,255,.1);
  padding:10px;
  display: flex;
  overflow: hidden;
    .login-container-box {
      width: 408px;
      border-radius: 10px;
      margin: 0 auto;
    }
  .login-container-right {
    width: 35%;
    text-align: center;
    color: #fff;
    position: relative;
    .login-container-right-icon {
      height: 600px;
    }
    .login-container-notice {
      position: absolute;
      bottom: 100px;
      right: 0;
      left: 0;
      text-align: center;
    }
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 24px;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
